package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.model.auto.NewsAccount;
import com.example.model.auto.NewsInfo;
import com.example.model.auto.UserInfo;
import com.example.service.INewsInfoService;
import com.example.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 新闻热点信息表 前端控制器
 * </p>
 *
 * @author 姚帅
 * @since
 */
@RestController
@RequestMapping("/news")
public class NewsInfoController {

    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private INewsInfoService iNewsInfoService;

    /*查询博客*/
    @GetMapping("/getNews")
    public List<NewsInfo> getNews(){
        return iNewsInfoService.list();
    }

    /*通过类型查询博客列表*/
    @PostMapping("/selectNewsByType/{Value}")
    public List<NewsAccount> selectNewsByType(@PathVariable("Value") String Value){
        return iNewsInfoService.selectNewsByType(Value);
    }

    /*通过内容查询博客列表*/
    @PostMapping("/selectNewsByAll/{Value}")
    public List<NewsAccount> selectNewsByAll(@PathVariable("Value") String Value){
        List<NewsAccount> newsAccountList = iNewsInfoService.selectNewsByAll(Value);
        return newsAccountList;
    }

    /*通过id查询博客*/
    @GetMapping("/getNewsById/{id}")
    public NewsInfo getNewsById(@PathVariable("id") int id){
        return iNewsInfoService.getById(id);
    }

    /*修改博客*/
    @PostMapping("/updateNews")
    public void updateNews(@RequestBody NewsInfo newsInfo){
        iNewsInfoService.updateById(newsInfo);
    }

    /*添加博客*/
    @PostMapping("/setNews")
    public void setNews(@RequestBody NewsInfo newsInfo){
        newsInfo.setNewsTime(new Date());
        iNewsInfoService.save(newsInfo);
    }

    /*删除博客*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        return iNewsInfoService.removeById(id);
    }

    /*联表查询*/
    @GetMapping("/selectNews")
    public List<NewsAccount> selectNews(){
        return iNewsInfoService.selectNews();
    }

    /*根据id查询资讯*/
    @GetMapping("/selectNewsById/{id}")
    public List<NewsAccount> selectNewsById(@PathVariable("id") int id){
        return iNewsInfoService.selectNewsById(id);
    }

    @GetMapping("/getNewsByUserId/{admin}")
    public List<NewsInfo> getNewsByUserId(@PathVariable("admin") String admin){
        QueryWrapper<NewsInfo> object = new QueryWrapper<>();
        object.eq("create_name", admin);
        return iNewsInfoService.list(object);
    }


}
